using System;
using Castle.DynamicProxy;
using EntityAop.Configuration;
using EntityAop.Interceptors.Support;

namespace EntityAop.Interceptors
{
    public class BaseInterceptor : IInterceptorWithRelatedTypes
    {
        public Type[] RelatedTypes { get; protected set; }

        public Func<object, object> MessageSendProcess { get; protected set; }

        protected EntityAopConfiguration Configuration { get { return EntityAopConfiguration.Instance; } }

        public BaseInterceptor()
        {
            
        }

        public void Intercept(IInvocation invocation)
        {
            this.PreProceed(invocation);
            this.PerformProceed(invocation);
            this.PostProceed(invocation);
        }

        protected virtual void PreProceed(IInvocation invocation)
        {
            EntityMetadataHelper.InitEntityMetadata(invocation);
        }

        protected virtual void PerformProceed(IInvocation invocation)
        {
            invocation.Proceed();
        }

        protected virtual void PostProceed(IInvocation invocation)
        {
            InstanceSpecificHelper.RegisterInverseReferenceType(invocation);
        }
    }
}